package com.hethongviet.preventingmachine.helps;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;

public final class SMSHelperDoor
{
	public static final char CODE_END = '#';
	public static final char CODE_SEPARATE = ',';

	public static final String CODE_SYNCHRONIZE_NUMBERS_CONTROL = "13";

	private static String phoneNumberDevice = null;

	private static Activity activity = null;

	public static String getDeviceNumber()
	{
		return phoneNumberDevice;
	}

	public static void setDeviceNumber(String number)
	{
		phoneNumberDevice = number;
	}

	public static void setActivity(Activity activity1)
	{
		activity = activity1;
	}

	// ---sends an SMS message to another device---
	private static void sendSMS(String number, String code, String message)
	{
		final String CODE_SENT = "SMS_SENT";
		final String CODE_DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPendingIntent = PendingIntent.getBroadcast(activity,
				0, new Intent(CODE_SENT), 0);

		PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(
				activity, 0, new Intent(CODE_DELIVERED), 0);

		// ---when the SMS has been sent---
		activity.registerReceiver(new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1)
			{
				switch (getResultCode())
				{
				case Activity.RESULT_OK:
					Utils.toast(activity, R.string.message_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Utils.toast(activity, R.string.message_error_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Utils.toast(activity,
							R.string.message_error_SMS_not_available);
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Utils.toast(activity, R.string.message_error_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Utils.toast(activity, R.string.message_error_SMS_radio_off);
					break;
				}
			}
		}, new IntentFilter(CODE_SENT));

		// ---when the SMS has been delivered---
		activity.registerReceiver(new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1)
			{
				switch (getResultCode())
				{
				case Activity.RESULT_OK:
					Utils.toast(activity, R.string.message_SMS_deliveried);
					break;
				case Activity.RESULT_CANCELED:
					Utils.toast(activity,
							R.string.message_error_SMS_not_deliveried);
					break;
				}
			}
		}, new IntentFilter(CODE_DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(number, null, message, sentPendingIntent,
				deliveredPendingIntent);

		Utils.debug("---------Sending message via SMS = " + message);
		Utils.debug("---------Sending message via SMS to " + number);

	}

	public static void synchronizeOfUser(String number)
	{
		Utils.debug("Send message sychronize to admin!!!!!!");
		if (number == null || number.equals(""))
		{
			Utils.debug("Number null!!!!!!");
			return;
		}

		sendSMS(number, CODE_SYNCHRONIZE_NUMBERS_CONTROL, "OK" + CODE_END);
	}

}
